package com.smm.imgServer.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smm.imgServer.domain.ImageProcesDTO;
import com.smm.imgServer.util.ImageProcesUtil;

/**
 * <p>
 * 图片处理
 * </p>
 * 
 * @author 曾世华
 * @date 2016年2月1日
 * @version 1.0
 * 
 */
@Controller
public class ImageProcesController {

	Logger log = Logger.getLogger(this.getClass());

	@Value("${upload.path}")
	private String path;
	
	@Value("${default.img}")
	private String defaultImg;

	@RequestMapping(value = "/imageProces")
	@ResponseBody
	public void imageProces(ImageProcesDTO imageProcesDTO,
			HttpServletRequest request,HttpServletResponse response) {
		log.info("请求带参数图片处理.");

		/**
		 * 检查原图是否存在
		 */
		String orgImage = getImagePath(imageProcesDTO.getImageName());

		File file = new File(orgImage);
		if (file.exists()) {
			String saveImagePath=getSaveImagePath(imageProcesDTO);
			String saveName=getSaveName(imageProcesDTO);
			String saveSuffix=getSaveSuffix(imageProcesDTO);
			/**
			 * 根据比例压缩图片
			 */
			ImageProcesUtil imageProcesUtil = new ImageProcesUtil(orgImage, saveImagePath,saveName,saveSuffix );
			try {
				imageProcesUtil.zoom(imageProcesDTO.getW(), imageProcesDTO.getH());
				File outFile=new File(saveImagePath+saveName+"."+saveSuffix);
				if(outFile.exists()){
					writeImg(outFile,response);
				}else{
					log.error(saveImagePath+saveName+"."+saveSuffix+" 压缩图片失败.");
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				log.error("读取默认图片出错.",e);
			}
		} else {
			log.info(" 图片不存在,输出默认图片.");
			try {
				File defaultFile=new File(defaultImg);
				writeImg(defaultFile,response);
			} catch (Exception e) {
				e.printStackTrace();
				log.error("读取默认图片出错.",e);
			}
			
		}

	}

	/**
	 * 原图路径
	 * @param imageName
	 * @return
	 */
	public String getImagePath(String imageName) {

		String first = imageName.substring(0, 1);
		String second = imageName.substring(1, 2);
		String orgImage = path + "/" + first + "/" + second + "/" + imageName;

		return orgImage;
	}

	/**
	 * 图片路径
	 * @param imageProcesDTO
	 * @return
	 */
	public String getSaveImagePath(ImageProcesDTO imageProcesDTO) {

		String first = imageProcesDTO.getImageName().substring(0, 1);
		String second = imageProcesDTO.getImageName().substring(1, 2);
		String saveImage = path + "/" + first + "/" + second + "/";

		return saveImage;
	}

	/**
	 * 图片名字
	 * @param imageProcesDTO
	 * @return
	 */
	public String getSaveName(ImageProcesDTO imageProcesDTO) {

		String newName = imageProcesDTO.getImageName().substring(0,
				imageProcesDTO.getImageName().lastIndexOf("."));
		String saveName = newName + "_" + imageProcesDTO.getW() + "_"
				+ imageProcesDTO.getH();

		return saveName;
	}

	/**
	 *  保存的图片后缀
	 * @param imageProcesDTO
	 * @return
	 */
	public String getSaveSuffix(ImageProcesDTO imageProcesDTO) {

		String suffix = imageProcesDTO.getImageName().substring(
				imageProcesDTO.getImageName().lastIndexOf(".")+1,
				imageProcesDTO.getImageName().length());

		return suffix;
	}
	
	public void writeImg(File file,HttpServletResponse response){
		try {
			InputStream in = new FileInputStream(file);
			OutputStream os = response.getOutputStream();
			byte[] b = new byte[1024];  
			while( in.read(b)!= -1){ 
				os.write(b);     
			}
			in.close(); 
			os.flush();
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("输出图片到页面发生错误", e);
		}
	}
}
